{# Form macros. Native inputs with ``name`` attributes, so every form posts or re-queries through htmx without client-side state. {% from "components/macros/form.html" import field, text_input, select, date_input, primary_button %} #} {% from "components/macros/icons.html" import icon %} {# Shared control styling: DaisyUI's control of ``kind`` (input, select, textarea, checkbox, radio) in its small form; the text controls sit bordered on the page surface so a bar of them reads as one. DaisyUI owns focus, disabled and placeholder states; the theme owns radius and color. Templates never spell a DaisyUI control class themselves. #} {# DaisyUI draws its own 2px outline on focus; the teal border is the focus cue here, so the outline goes. #} {% set _surface = "bg-aegis-bg text-aegis-text focus:border-aegis-teal focus:outline-none focus-within:outline-none [&:user-invalid]:border-error" %} {# Class names are spelled in full: Tailwind emits only what it can read from the source, so a name built from parts would never compile. #} {% macro control(kind) -%} {{ { "input": "input input-bordered input-sm " ~ _surface, "select": "select select-bordered select-sm " ~ _surface, "textarea": "textarea textarea-bordered textarea-sm " ~ _surface, "checkbox": "checkbox checkbox-primary checkbox-xs", "radio": "radio radio-primary radio-xs", }[kind] }} {%- endmacro %} {# Buttons are DaisyUI's: outline so the data stays louder than the controls; ``tone`` is default (quiet: muted text and border, which DaisyUI has no outline variant for), primary (teal), danger or warn; ``size`` sm for a form's verbs, xs for a row's. Hover, focus, disabled and the press scale come with it; the theme owns radius and color. #} {% macro button_classes(tone="primary", size="sm") -%} {% set tones = {"default": "border-aegis-border text-aegis-muted hover:border-aegis-muted hover:text-aegis-text hover:bg-aegis-border/20", "primary": "btn-primary", "danger": "btn-error", "warn": "btn-warning"} %} btn btn-outline {{ {"sm": "btn-sm", "xs": "btn-xs"}[size] }} {{ tones.get(tone, tones["default"]) }} {%- endmacro %} {# Inline primary button (no loading spinner). Defaults to ``type="button"`` so it doesn't accidentally submit a form it's nested inside — pass ``type="submit"`` explicitly when you want submit semantics. ``attrs`` lets the caller pass other attributes (Alpine ``@click``, ``:disabled``, ``aria-*``), never a ``type=`` (refused). ``filled`` is the page's one action. #} {% macro primary_button(label, type="button", attrs="", filled=False) %} {% endmacro %} {# Primary submit button with an inline loading spinner. Parent scope must expose ``loading`` (bool) — typically flipped by ``@submit="loading = true"`` on the form. The button disables itself while loading so a double-click can't double-submit. #} {% macro submit_button(idle_label, loading_label) %} {% endmacro %} {# A native select. ``options`` are objects or dicts with ``id`` and ``name``, or, ``grouped``, ``{"label", "options"}`` groups of them (an optgroup each); ``blank`` is the "no filter" label (``None`` for no blank choice), chosen while nothing is; ``selected`` the current id. ``label`` wraps it inline for a filter bar; ``block`` lays it out full-width inside a ``field()``; ``attrs`` rides on the select. #} {% macro select(name, options, selected=None, blank="All", label=None, block=False, grouped=False, attrs="") %} {% if label %}{% endif %} {% endmacro %} {% macro date_input(name, value=None, label=None, block=False) %} {% if label %}{% endif %} {% endmacro %} {# Every file picker: the native input hidden behind a button-styled label, so it looks like every other button. Picking submits its form (an upload button); with ``submit=False`` it keeps what was picked (a second pick adds to the first, where ``multiple``), says how many and how big, and lists each, removable (the ``filePicker`` component in app.js), for a form with its own submit. #} {% macro file_button(id, label, accept=None, name="file", submit=True, required=True, multiple=False) %} {%- set button = button_classes("primary", "xs") -%} {%- set input -%}{%- endset -%} {%- set picker -%}{{ input }}{%- endset -%} {% if submit %} {{ picker }} {% else %}
{{ picker }}Nothing chosen
{% endif %} {% endmacro %} {% macro checkbox(name, checked=False, label=None, value=None, attrs="") %} {% endmacro %} {# A dropdown of checkboxes, posting ``name`` once per tick: a multi-pick filter. ``options`` are dicts with ``value``, ``label``, ``checked`` and an optional ``count``, and may carry their own ``name`` (a container under its service, ``indent``ed). Kept across the swaps of the form it sits in (``hx-preserve``, which needs the ``id``), so the menu stays open while several are ticked; a click anywhere else closes it. The button at its top ticks every option, or clears them when all are ticked, and the form refreshes as for any tick. #} {% macro checklist(id, name, label, options) %} {% set ticked = options|selectattr("checked")|list|length %}
{{ label }}{% if ticked %}: {{ ticked }}{% endif %}
{{ label }}
{% for o in options %} {% endfor %}
{% endmacro %} {# A search over what is already on the page: as you type it narrows ``target``'s rows to those holding the text and marks each match, with no request, and rows that arrive later follow it (app.js ``data-filter``). For a list the page holds whole (Logs); a list the server pages through searches there instead (``search_input``). Its ``name`` keeps the text in the URL, so a reload or another filter keeps it; the form that holds it leaves it out of its triggers (``change[!target.closest('[data-filter]')]``). #} {% macro filter_input(target, name="q", value=None, placeholder="Filter") %} {% endmacro %} {# A stable id lets htmx restore focus when a search result replaces the form. #} {% macro search_input(name, value=None, placeholder="Search", autofocus=False, attrs="") %} {% endmacro %} {# A labelled field. The control comes via {% call %}; ``error`` renders under it (pattern 1's 422 re-render passes field errors here); ``hint`` says what the control takes (its ``pattern``, its ``required``), shown only once what was typed does not fit, before anything is sent. #} {% macro field(label, error=None, hint=None) %}
{% if error %}

{{ error }}

{% endif %} {% if hint %}{% endif %}
{% endmacro %} {# A dialog or drawer form is sections then a footer: each part of what is being done its own section, divided from the one before (``title`` and a ``hint`` such as "optional" over it, or neither for a plain run of fields), and the footer at the bottom right. #} {% macro form_section(title=None, hint=None) %}
{% if title %}

{{ title }}

{% if hint %}{{ hint }}{% endif %}
{% endif %} {{ caller() }}
{% endmacro %} {# Closing the dialog it is in, unchanged: every dialog's Cancel. #} {% macro cancel_button() %} {%- endmacro %} {# A dialog or drawer form's last row, at the bottom right: Cancel (closing the dialog it is in; ``cancel=False`` for a form inside a drawer's card, which closing would not suit), then the submit. What its caller gives (a Remove) sits at the left. In a ``wizard`` it walks the steps: Back, Next, and the submit only on the last. #} {% macro form_footer(label, cancel=True, attrs="", wizard=False) %}
{% if caller is defined %}
{{ caller() }}
{% endif %} {% if cancel %}{{ cancel_button() }}{% endif %} {% if wizard %} {{ primary_button("Next", "button", '@click="next()"') }} {{ primary_button(label, "submit", attrs) }} {% else %} {{ primary_button(label, "submit", attrs) }} {% endif %}
{% endmacro %} {# A form in steps (the ``wizard`` component in app.js), for one with much to pick from: its steps down the left, numbered (a check once passed, with its answer, ``summary``, an Alpine expression), each a click back once reached; the step at hand on the right (each in ``wizard_step``), and ``form_footer(..., wizard=True)`` walking them; Enter in a field is Next until the last step. ``steps`` are (title, summary or None) pairs. #} {% macro wizard(steps) %}
    {% for title, summary in steps %}{% set i = loop.index0 %}
  1. {% endfor %}
{{ caller() }}
{% endmacro %} {# One step of a ``wizard``, numbered from 0: shown while it is at hand. #} {% macro wizard_step(n) %}
{{ caller() }}
{% endmacro %} {# ``block`` (the default) fills a ``field()``; inline, it sits in a bar beside its buttons, as ``select`` and ``date_input`` do. #} {% macro text_input(name, value=None, placeholder="", required=False, autofocus=False, pattern=None, attrs="", block=True) %} {% endmacro %} {% macro number_input(name, value=None, step=1, min=None, max=None, required=False) %} {% endmacro %} {% macro money_input(name, value=None, placeholder="0.00") %}
{% endmacro %} {% macro textarea(name, value=None, rows=6, placeholder="", attrs="") %} {% endmacro %} {# Themed combobox — replaces native {% endif %}
{% if grouped %} {% else %} {% endif %}
{% endmacro %} {# Password input with a show/hide eye toggle. Standalone so callers can wire it up with either ``name=`` (plain form post) or Alpine ``x_model`` bindings. The toggle swaps the rendered ``type`` via ``:type`` so browser password managers still recognize the field as a password — Alpine's ``:type`` takes effect after the initial parse, so autofill works. ``bg_class`` lets callers match the surrounding surface. #} {% macro password_input(name=None, x_model=None, autocomplete=None, required=True, minlength=None, placeholder="", bg_class="bg-aegis-card") %}
{% endmacro %} {# Attributes that keep a browser and the password managers from filling an input: a pasted key, or a field no person should fill. #} {% macro no_autofill() %}autocomplete="off" data-1p-ignore data-lpignore="true" data-bwignore data-form-type="other"{% endmacro %} {# The trap every public form carries (``backend/security/honeypot.py``): an input a person never sees or tabs to, and a form-filler bot fills. Out of sight rather than ``display:none``, which bots skip, and closed to the password managers that would fill it for a person. #} {% macro honeypot() %} {% endmacro %} {# "OR" divider with hairlines — sits between a form and an alternate action (e.g. email/password above, OAuth buttons below). #} {% macro or_divider() %}
or
{% endmacro %} {# The time-window chips: a row of exclusive pills that submits as one radio field, so a click re-requests the enclosing form. Same recipe as ``layout.chip``, driven by ``peer-checked`` because a radio rather than a link marks the choice. ``ranges`` are ``(value, label)``, or ``(value, label, icon)`` for a chip that shows its icon (``icons.html``), its label on hover and to a reader. ``multiple`` makes them ticks instead, ``days`` the values ticked, the row a ``data-checklist``: over a list it narrows the rows with no request (``data-facets`` in app.js). #} {% macro range_chips(ranges, days, field="days", aria="Time window", multiple=False) %}
{% for choice in ranges %} {% set value, label = choice[0], choice[1] %} {% set glyph = choice[2] if choice|length > 2 else none %} {% endfor %}
{% endmacro %} {# The time-window row that re-requests ``url`` when a chip is picked and swaps ``target`` (``hx_replace``), the choice kept in the address as ``field``. #} {% macro range_form(ranges, current, url, target, field="days", aria="Time window", id=None, class="") %}
{{ range_chips(ranges, current, field=field, aria=aria) }}
{% endmacro %} {# A small action button (row verbs, card verbs, toolbar). ``attrs`` is the htmx verb, never a ``type=`` (refused: pass ``type``); ``vals`` a dict for ``hx-vals``; ``tone`` is default (quiet), primary (teal), danger or warn; ``glyph`` draws an icon in place of the label, which then names it for a reader and on hover (as the chips do); ``type`` is ``submit`` for a form's own verb; ``href`` makes it a link (a download: ``attrs`` "download"), looking the same. The one recipe every compact button uses. #} {% macro action(label, attrs, tone="default", vals=None, glyph=None, type="button", href=None) %} {% set classes = "action " ~ button_classes(tone, "xs") ~ " whitespace-nowrap" %} {% if href %}{{ label }}{% else %} {% endif %} {% endmacro %}